package com.hs.controller;


import com.hs.config.R;
import com.hs.entity.Student;
import com.hs.entity.User;
import com.hs.entity.YxClass;
import com.hs.service.StudentService;
import com.hs.service.YxClassService;
import com.hs.tool.StringTools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("yxClass")
public class YxClassController {
    @Autowired
    YxClassService yxClassService;
    @RequestMapping(value = "insert",produces="application/json;charset=utf-8")
    @ResponseBody
    public R insert(HttpSession session,YxClass record, Model model) {
        if (record == null) {
//            model.addAttribute("msg","请填写用index户名密码等关键信息");
            return R.FAILURE("请填写班级信息", null);
        }
        if (StringTools.isEmpty(record.getName())) {
            model.addAttribute("msg", "请填写班级名称");
            return R.FAILURE("请填写班级名称", null);
        }
            int count = yxClassService.selectByConditionCount(record);
            if (count > 0) {
                return R.FAILURE("班级名称已存在", null);
            }
            User user = (User) session.getAttribute("user");
            record.setUserId(user.getId());
            int i = yxClassService.insertSelective(record);
            if (i > 0) {
                return R.SUCCESS("", i);
            }
            return R.FAILURE("", 0);
        }

    }

